const string = {
	/**
	 * 获得一串随机字符串
	 * @param range
	 * @returns {string}
	 */
	random: function(range) {
		var str = "",
			arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
				'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
				'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
				'U', 'V', 'W', 'X', 'Y', 'Z'
			];
		var pos = '';
		for (var i = 0; i < range; i++) {
			pos = Math.round(Math.random() * (arr.length - 1));
			str += arr[pos];
		}
		return str;
	},
	/**
	 * 数据脱敏
	 * @param str 要脱敏的字符串
	 * @param headLength 头部长度
	 * @param tailLength 尾巴长度
	 * @param placeholderLength 占位符长度
	 * @returns {string}
	 */
	desensitiz: function(str, headLength, tailLength, placeholderLength) {
		headLength = headLength || 3;
		tailLength = tailLength || 4;
		placeholderLength = placeholderLength || 4;
		let newStr = str.substr(0, headLength);
		for (let i = 0; i < placeholderLength; i++) {
			newStr += '*';
		}
		newStr += str.substr(-tailLength);
		return newStr;
	},
	dateFormat: function(date, formatStr = "{Y}-{MM}-{DD}") {
		let dateObj = {},
			rStr = /\{([^}]+)\}/,
			mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];

		dateObj["Y"] = date.getFullYear();
		dateObj["M"] = date.getMonth() + 1;
		dateObj["MM"] = dateObj["M"] < 10 ? "0" + dateObj["M"] : dateObj["M"];
		dateObj["Mon"] = mons[dateObj['M'] - 1];
		dateObj["D"] = date.getDate();
		dateObj["DD"] = dateObj["D"] < 10 ? "0" + dateObj["D"] : dateObj["D"];
		dateObj["h"] = date.getHours();
		dateObj["hh"] = dateObj["h"] < 10 ? "0" + dateObj["h"] : dateObj["h"];
		dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
		dateObj["tt"] = dateObj["t"] < 10 ? "0" + dateObj["t"] : dateObj["t"];
		dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
		dateObj["i"] = date.getMinutes();
		dateObj["ii"] = dateObj["i"] < 10 ? "0" + dateObj["i"] : dateObj["i"];
		dateObj["s"] = date.getSeconds();
		dateObj["ss"] = dateObj["s"] < 10 ? "0" + dateObj["s"] : dateObj["s"];

		while (rStr.test(formatStr)) {
			formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
		}
		return formatStr;
	}
}
export default string;
